8
Beginner’s Guide to Code Algorithms
8
their heights by placing them side by side. If one is higher, you pour a little bit from
the glass with the greater height to the glass with the lesser height.
A statement is usually an instruction to do something—the most common being
moving data from one variable to another. In the glass example above, pouring water
from one glass to another in an attempt to equalize is a statement.
A condition is a decision point that helps the situation direct the execution of a
statement from a choice of more than one statement. In our example above, checking
the water level in one glass is higher than the other is a condition. That helps one
decide whether to pour the water from the left glass to the right, or from the right glass
to the left. Pouring water from one glass to another is a conditional statement. Pouring
from left to right is one statement, from right to left is another, and the condition helps
us decide which one should be done.
Chances are that just one pour will not equalize the glasses. If you are like me,
as soon as I have poured the higher to the lower, the lower now becomes higher!
So, we need to do the exact opposite, until we find that the heights are the same.
This is known as a repetition. Often described in programming languages as “Do
Loops”, “While Loops” and “Do Untils”—this is the critical part of an algorithm that
is immensely useful for a program—because it is the repetitive work that a computer
program does best, by relieving the human being from this task.
Now let us look at a problem that we can solve using a spreadsheet to understand
these six structures in business terms.
Imagine that I am a sales executive in a shoe company that operates in three states.
I need to send a report to my boss that summarizes the sales for all three states in one
spreadsheet. I have each state in a separate spreadsheet as shown below.
SPREADSHEET A
FIGURE 1.2 Sales figures by state.